﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using CameraAPI;
using cvLib;

namespace PTZCameraCapture
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();

			InitPTZCams();
			
			//ourMethod = new OurMethod();
			//piThread = new Thread(new ThreadStart(CalcPiThreadStart));
        }

		private void InitPTZCams()
		{
			String IPAddr_static = "172.26.187.141"; //"172.26.186.188";//
			String IPAddr_left = "172.26.186.188";
			String IPAddr_back = "172.26.187.135";
			String IPAddr_right = "172.26.186.216";

			String[] ptzCommands;

			//PTZ camera 1
			nstate = 3;
			currentState = 0;
			ptzCommands = new String[nstate];

			ptzCommands[0] = "&pan=17.5474&tilt=-36.5500&zoom=2386";//&autofocus=on&autoiris=on";
			ptzCommands[1] = "&pan=-9.2982&tilt=-35.5500&zoom=2385";
			ptzCommands[2] = "&pan=-32.0199&tilt=-33.5250&zoom=2385";

			//Connecting Camera
			ptz_Static = new Camera(IPAddr_static, ptzCamStatic, ptzCommands, 0, pB_Static);
			ptz_Static.ConnectIpCam("prabhu", "welcome", "mjpeg", true);
			ptz_Left = new Camera(IPAddr_left, ptzCamLeft, ptzCommands, 0, pB_Left);
			ptz_Left.ConnectIpCam("prabhu", "welcome", "mjpeg", true);
			ptz_back = new Camera(IPAddr_back, ptzCamBack, ptzCommands, 0, pB_Back);
			ptz_back.ConnectIpCam("prabhu", "welcome", "mjpeg", true);
			ptz_right = new Camera(IPAddr_right, ptzCamRight, ptzCommands, 0, pB_Right);
			ptz_right.ConnectIpCam("prabhu", "welcome", "mjpeg", true);

		}
		private void InitTracker()
		{
			// Tracker
			rectCount = 0;
			onProcess = false;
			trackedObject = false;
			drawRect = false;
			tmpSelection = new Rect(0, 0, 0, 0);
		}

		private  void button1_Click(object sender, EventArgs e)
        {
			//ourMethod.createWindows();
			//ourMethod.initTracker();
			
			//cvLib::on_mouse(0,0,0,0);
			//String msg="";
			//Byte[] a ;
		
			//a = ptz_Static.getImageByte();
			//c1.getImagefromCS(a, a.GetLength(0));
			//msg = c1.getImagefromCS(a, a.GetLength(0)).ToString();
			//MessageBox.Show(msg);				

			//ptz_Left.PanTilt(2);
			//String msg;
			//msg = ptz_right.GetCurrentPosition();
			//MessageBox.Show(msg);
        }


		//PTZ Camera
		Camera ptz_Static, ptz_Left, ptz_back, ptz_right;		
		public static int nstate;		
		public int currentState;

		// --- status variables ---//
		bool onProcess;
		bool trackedObject;
		bool drawRect;

		// --- count variables ---//
		int rectCount;
		const int maxNumberRect = 5;
		Rect tmpSelection;

		Thread cppThread; 

		// Test for OpenCV
		//Class1 c1;
		OurMethod ourMethod;
		bool initilized = false;


		private void CalcPiThreadStart()
		{
			//String msg = "";
			Byte[] imageBuf;

			
			ourMethod.methodRunLocal();

			//while (true)
			//{
			//    imageBuf = ptz_Static.getImageByte();
			//    ourMethod.methodRun(imageBuf, imageBuf.GetLength(0));
			//    //ourMethod.getImagefromCS(imageBuf, imageBuf.GetLength(0));
			//    //msg = ourMethod.getImagefromCS(a, a.GetLength(0)).ToString();
			//    //MessageBox.Show(msg);
			//    //System.Threading.Thread.Sleep(300);
			//}



		}
		private void btnInit_Click(object sender, EventArgs e)
		{
			ourMethod = new OurMethod();
			cppThread = new Thread(new ThreadStart(CalcPiThreadStart));
			initilized = true;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (initilized)
			{
				ourMethod.initTracker();
				cppThread.Start();
			}
			else
			{
				MessageBox.Show("Initialize first...");
			}						
		}

		private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (initilized)
			{
				cppThread.Abort();
			}			
		}		
    }
}
